# 如果损失值变化小于1e-2，停止迭代
# 1.sklearn    pytorch
#
# 1.1     使用 boston房价预测
#
# sklearn操作
# 加载数据
# 归一化
# 数据切分
# 使用pca降维到4维
#
# pytorch
# 模型创建
# 使用rmsprop优化器，配合合理学习率迭代次数
# 让模型得到最优化效果
# 绘制代价损失图
# 绘制模型测试集的预测结果和实际值折线图
# 打印测试集的mse数值

from sklearn.datasets import load_boston
from sklearn.preprocessing import MinMaxScaler
from sklearn.decomposition import PCA
from sklearn.model_selection import train_test_split
# 1.sklearn    pytorch
#
# 1.1     使用 boston房价预测
#
# sklearn操作
data = load_boston()
x = data.data
y = data.target
# 加载数据
# 归一化
x = MinMaxScaler().fit_transform(x)

# 使用pca降维到4维
x = PCA(n_components=4).fit_transform(x)
print(x.shape)

# 数据切分
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=.3)


# pytorch
import torch
from torch import nn
x_train = torch.autograd.Variable(torch.Tensor(x_train))
x_test = torch.autograd.Variable(torch.Tensor(x_test))
y_train = torch.autograd.Variable(torch.Tensor(y_train))
y_test = torch.autograd.Variable(torch.Tensor(y_test))
# 模型创建
model = nn.Linear(4, 1, bias=True)
mse = nn.MSELoss()
# 使用rmsprop优化器，配合合理学习率迭代次数
opt = torch.optim.RMSprop(model.parameters(), lr=1e-1)
# 让模型得到最优化效果
epochs = 3000
loss = []
for step in range(epochs):
    opt.zero_grad()
    h = model(x_train)
    loss_ = mse(h, y_train)
    loss_.backward()
    opt.step()
    loss.append(loss_.data.numpy())
    if step % 20 == 0:
        print(step, loss_.data.numpy())
# 绘制代价损失图
import matplotlib.pyplot as plt
plt.plot(loss)
plt.show()
# 绘制模型测试集的预测结果和实际值折线图
y_ = model(x_test).data.numpy()
# 打印测试集的mse数值
